package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.UnLikesBehaviorDto;
import com.heima.behavior.pojos.ApLikesBehavior;
import com.heima.behavior.pojos.ApUnlikesBehavior;
import com.heima.behavior.service.ApUnLikesBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApThreadLocalUtil;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Gareth Cao
 * @date 2022/6/25 17:43
 */
@Service
public class ApUnLikesBehaviorServiceImpl implements ApUnLikesBehaviorService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ResponseResult unLike(UnLikesBehaviorDto dto) {
        //1 校验参数
        if (dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2 获取登录用户
        ApUser apUser = ApThreadLocalUtil.getUser();
        if (apUser == null) {
            //未登录
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (apUser.getId() == 0) {
            //游客
            return ResponseResult.errorResult(AppHttpCodeEnum.VISITOR_NEED_LOGIN);
        }
        //3 判断是否存在key
        //3.1 定义不喜欢行为的redis中hash存储数据的大key
        String unlikes_key = "USER_UNLIKES_" + dto.getArticleId();
        Boolean flag = stringRedisTemplate.opsForHash().hasKey(unlikes_key, apUser.getId() + "");
        //4 如果已经不喜欢，并正在执行不喜欢操作，那么直接返回
        if (flag && dto.getType() == 0) {
            return ResponseResult.okResult(AppHttpCodeEnum.UNLIKE_EXIST);
        }
        //5 如果已经不喜欢了，并执行取消不喜欢，则删除redis和mongo
        if (flag && dto.getType() == 1) {
            //删除redis
            stringRedisTemplate.opsForHash().delete(unlikes_key, apUser.getId() + "");
            //删除mongo
            Query query = Query.query(Criteria.where("userId").is(apUser.getId())
                    .and("articleId").is(dto.getArticleId()));
            mongoTemplate.remove(query, ApUnlikesBehavior.class);
            return ResponseResult.okResult(AppHttpCodeEnum.UNLIKE_CANCEL);
        }
        //6 未不喜欢并执行不喜欢操作，则添加redis和mongo
        if (!flag && dto.getType() == 0) {
            //添加redis
            stringRedisTemplate.opsForHash().put(unlikes_key, apUser.getId() + "", "1");
            //添加mongo
            ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
            apUnlikesBehavior.setId(ObjectId.get().toHexString());
            apUnlikesBehavior.setUserId(apUser.getId().longValue());
            apUnlikesBehavior.setArticleId(dto.getArticleId());
            apUnlikesBehavior.setType((short) 0);
            apUnlikesBehavior.setCreatedTime(new Date());
            mongoTemplate.insert(apUnlikesBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.UNLIKE_SUCCESS);
        }
        return null;
    }
}
